package it.polimi.travelDream.ejb.servlet;

import it.polimi.travelDream.ejb.interfaces.TransportationMgrRemote;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Map;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class TravelServlet
 */
@WebServlet("/AddTransportation")
public class AddTransportation extends HttpServlet {
	private static final long serialVersionUID = 1L;
	DateFormat format = new SimpleDateFormat("dd/MM/yyyy");
	@EJB
	TransportationMgrRemote umr;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AddTransportation() {
		super();
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Map<String, String[]> mappa = request.getParameterMap();
		format.setLenient(false);
		/*Empty Fields Check*/
		if (mappa.get("typeTransportation")[0].isEmpty() || mappa.get("departureFrom")[0].isEmpty() || mappa.get("goingTo")[0].isEmpty() || mappa.get("outBound")[0].isEmpty() || mappa.get("inBound")[0].isEmpty() ||	mappa.get("cost")[0].isEmpty())
		 {
			request.setAttribute("message","Empty fields.");
			request.getRequestDispatcher("/BPAddTransportation.jsp").forward(request, response); 
		 }
				else
		{	
			
			try {
				 format.parse(mappa.get("outBound")[0]);
				 format.parse(mappa.get("inBound")[0]);
				 Integer.parseInt(mappa.get("cost")[0]);
				 if (
						 format.parse(mappa.get("outBound")[0]).before(format.parse(mappa.get("inBound")[0])) )
						 
						 {
				 /*Transportation Insertion*/
					umr.addTransportation(mappa.get("typeTransportation")[0], mappa.get("departureFrom")[0],mappa.get("goingTo")[0], mappa.get("outBound")[0],mappa.get("inBound")[0],mappa.get("cost")[0]);
					/*Next JSP parameters and call */
					request.setAttribute("message", mappa.get("typeTransportation")[0]+" for "+ mappa.get("goingTo")[0] +" succesfully added.");
					request.getRequestDispatcher("/BPAdvice.jsp").forward(request, response); 
				 }
				 else {request.setAttribute("message","The departure date must be greater than the arrival date");
						request.getRequestDispatcher("/BPAddTransportation.jsp").forward(request, response);
				 }
				 } catch (ParseException e) {
				 
					 request.setAttribute("message","Date must be in dd/mm/yyyy format.");
						request.getRequestDispatcher("/BPAddTransportation.jsp").forward(request, response); 
					 
				 }
			
			
			
			
		}
	}

}
